package controller;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import bean.TcImg;
import service.GoodsDAO;

import java.util.Set;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class Tools {
	public static void main(String[] args){
		String s = "{js:[{id:\"110000\",\"city\":\"北#001京市\"},{id:\"110000\",\"city\":\"北#002京市\"}"  
                + ",{id:\"110000\",\"city\":\"北#002京市\"},{id:\"110000\",\"city\":\"北#002京市\"},"  
                + "{id:\"110000\",\"city\":\"#006北#005京市\"},"  
                + "{id:\"110000\",\"city\":\"北#002京市\"},"  
                + "{id:\"110000\",\"city\":\"北#002京市\"},{id:\"120000\",\"city\":\"天#009津市\"}]}";  
          
        JSONObject object = JSON.parseObject(s);  
        Object jsonArray = object.get("js");  
        System.out.println(jsonArray);  
//        List<Weibo> list = JSON.parseArray(jsonArray+"", Weibo.class);  
//        for (Weibo weibo : list) {  
//            System.out.println(weibo.getCity());  
//        }  
//		
//		String str="[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]";
//		System.out.println(str);
//		JSONArray obj=JSONObject.parseArray(str);
//		JSONObject obj2=JSONObject.parseObject(obj.get(0).toString());
//		System.out.println(obj2["a"]);
		/*int sum=0;
		System.out.print(sum);
		System.out.print("+"+1);
		System.out.print("+"+1);
		sum+=2;
		for(int i=3;i<=100;i+=2) {
			for(int j=1;j<=i;j++) {
				System.out.print("+"+i);
				sum+=i;
			}
		}
		System.out.println("="+sum);*/
		
		
		
//		int i=2;
//		short st=23;
//		String s="aaa";
//		double db=2.326;
//		boolean b=false;
//		Date date=new java.sql.Date(new java.util.Date().getTime());
//		Timestamp ts=new Timestamp(date.getTime());
//		A a=new A(s, i, db, st, date, ts, b);
//		Map hm;
//		try {
//			hm = getStringProperties(a);
//			Iterator<Map.Entry> it=hm.entrySet().iterator();
//			while(it.hasNext()) {
//				Map.Entry en=it.next();
//				String name=(String) en.getKey();
//				Object value=en.getValue();
//				if(value==null) {
////					System.out.println(name+"没有属性值");
//					continue;
//				}
////				System.out.println(name+":"+value+"-"+value.getClass().getName());
//			}
//		} catch (IllegalArgumentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (NoSuchMethodException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SecurityException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}
	
	//根据传进来的对象返回所有的属性名和属性值键值对的map集合
	public static Map getStringProperties(Object obj) throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, SecurityException, InvocationTargetException {
		 Map hm=new TreeMap();
		 //获取实体类的所有属性，返回Field数组
		 Field[] fields = obj.getClass().getDeclaredFields();
		 for(int i=0;i<fields.length;i++) {//获取实体类的所有属性，返回Field数组
			 String name = fields[i].getName(); //获取属性的名字
			 //将属性的首字符大写，方便构造get，set方法
			 name = name.substring(0,1).toUpperCase()+name.substring(1);
			 //获取属性的类型
			 String type = fields[i].getGenericType().toString(); 
			 //获得getter方法
			 Method m = obj.getClass().getMethod("get"+name);
//			 System.out.println(name+"==="+type);
			 switch(type) {
			 	case "class java.lang.String":
			 		String stringValue = (String) m.invoke(obj);
			 		hm.put(name, stringValue);
			 		break;
			 	case "int":
			 		int intValue = (int) m.invoke(obj);
			 		hm.put(name, intValue);
			 		break;
			 	case "short":
			 		Short shortValue = (Short) m.invoke(obj);
			 		hm.put(name, shortValue);
			 		break;
			 	case "double":
			 		Double doubleValue = (Double) m.invoke(obj);
			 		hm.put(name, doubleValue);
			 		break;
			 	case "class java.lang.Boolean":
			 		Boolean booleanValue = (Boolean) m.invoke(obj);
			 		hm.put(name, booleanValue);
			 		break;
			 	case "class java.sql.Date":
			 		Date dateValue = (Date) m.invoke(obj);
			 		hm.put(name, dateValue);
			 		break;
			 	case "class java.sql.Timestamp":
			 		Timestamp timestampValue = (Timestamp) m.invoke(obj);
			 		hm.put(name, timestampValue);
			 		break;
			 }
		 }
		 return hm;
	}
}

class A{
	private String s;
	private int i;
	private double db;
	private short st;
	public short getSt() {
		return st;
	}
	public A(String s, int i, double db, short st, Date date, Timestamp ts, Boolean b) {
		super();
		this.s = s;
		this.i = i;
		this.db = db;
		this.st = st;
		this.date = date;
		this.ts = ts;
		this.b = b;
	}
	public void setSt(short st) {
		this.st = st;
	}
	private Date date;
	private Timestamp ts;
	private Boolean b;
	public A() {}
	public String getS() {
		return s;
	}
	public void setS(String s) {
		this.s = s;
	}
	public int getI() {
		return i;
	}
	public void setI(int i) {
		this.i = i;
	}
	public double getDb() {
		return db;
	}
	public void setDb(double db) {
		this.db = db;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Timestamp getTs() {
		return ts;
	}
	public void setTs(Timestamp ts) {
		this.ts = ts;
	}
	public Boolean getB() {
		return b;
	}
	public void setB(Boolean b) {
		this.b = b;
	}
	
	
	
}
